function bsearch(arr, value) {
    const n = arr.length;
    let low = 0;
    let high = n - 1;

    while (low <= high) {
        const mid = Math.floor((low + high) / 2);
        if (arr[mid] === value) {
            return mid;
        }

        if (arr[mid] < value) {
            low = mid + 1;
        } else if (arr[mid] > value) {
            high = mid - 1;
        }
    }

    return -1;
}

console.log(bsearch([1, 2, 3, 7, 9], 7));
